﻿using System;
using System.Web;
using System.Collections;
using System.Web.Caching;

namespace IHISCETeim.Library
{
    /// <summary>
    /// 默认缓存管理类使用System.Web.HttpRuntime.Cache
    /// </summary>
    public class DefaultCacheStrategy : ICacheStrategy
    {
        protected static volatile System.Web.Caching.Cache webCache =System.Web.HttpRuntime.Cache;
        
        // 默认缓存存活期为1440分钟(24小时)
        protected int _timeOut = 1440; 

        /// <summary>
        /// 获取WebCache对象
        /// </summary>
        public static System.Web.Caching.Cache GetWebCacheObj
        {
            get { return webCache; }
        }
        /// <summary>
        /// 设置到期相对时间[单位:分钟] 
        /// </summary>
        public virtual int TimeOut
        {
            set { _timeOut = value > 0 ? value : 6000; }
            get { return _timeOut > 0 ? _timeOut : 6000; }
        }       

        /// <summary>
        /// 加入当前对象到缓存中,并使用依赖键
        /// </summary>
        /// <param name="objId">key for the object</param>
        /// <param name="o">object</param>
        public void Add(string CacheKey, object o)
        {
            if (CacheKey == null || CacheKey.Length == 0 || o == null)
            {
                return;
            }

            CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);

            CacheDependency dep = new CacheDependency(HttpContext.Current.Server.MapPath("/CacheDependencyFiles"));//建立缓存依赖项dp ;

            webCache.Insert(CacheKey, o, dep, System.DateTime.Now.AddMinutes(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);          
        }

        public void Add(string CacheKey, object o, int timeoutminute)
        {
            if (timeoutminute <= 0)
                timeoutminute = TimeOut;
            if (CacheKey == null || CacheKey.Length == 0 || o == null)
            {
                return;
            }

            CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);

            if (timeoutminute == 6000)
            {
                webCache.Insert(CacheKey, o, null, DateTime.MaxValue, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, callBack);
            }
            else
            {
                webCache.Insert(CacheKey, o, null, DateTime.Now.AddMinutes(timeoutminute), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);
            }
        }

        /// <summary>
        /// 此方法和Add(string CacheKey, object o)效果一样
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="o"></param>
        /// <param name="dependKey"></param>
        public void Add(string CacheKey, object o, string[] dependKey)
        {
            if (CacheKey == null || CacheKey.Length == 0 || o == null)
            {
                return;
            }

            CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);

            CacheDependency dep = new CacheDependency(HttpContext.Current.Server.MapPath("/CacheDependencyFiles"));//建立缓存依赖项dp ;

            webCache.Insert(CacheKey, o, dep, System.DateTime.Now.AddMinutes(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack); 
        }
        /// <summary>
        /// 删除缓存对象
        /// </summary>
        /// <param name="objId">对象的关键字</param>
        public void Remove(string CacheKey)
        {
            //objectTable.Remove(objId);
            if (CacheKey == null || CacheKey.Length == 0)
            {
                return;
            }
            webCache.Remove(CacheKey);
        }


        /// <summary>
        /// 返回一个指定的对象
        /// </summary>
        /// <param name="objId">对象的关键字</param>
        /// <returns>对象</returns>
        public object Get(string CacheKey)
        {

            if (CacheKey == null || CacheKey.Length == 0)
            {
                return null;
            }

            return webCache.Get(CacheKey);
        }

        /// <summary>
        /// 建立回调委托的一个实例
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="reason"></param>
        public virtual void onRemove(string key, object val, CacheItemRemovedReason reason)
        {

           // System.IO.File.WriteAllText(HttpContext.Current.Server.MapPath("Cachelog.txt"), "缓存移除");
           // return;
            //switch (reason)
            //{
            //    case CacheItemRemovedReason.DependencyChanged:
            //        break;
            //    case CacheItemRemovedReason.Expired:
            //        {
            //            //CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(this.onRemove);

            //            //webCache.Insert(key, val, null, System.DateTime.Now.AddMinutes(TimeOut),
            //            //    System.Web.Caching.Cache.NoSlidingExpiration,
            //            //    System.Web.Caching.CacheItemPriority.High,
            //            //    callBack);
            //            break;
            //        }
            //    case CacheItemRemovedReason.Removed:
            //        {
            //            break;
            //        }
            //    case CacheItemRemovedReason.Underused:
            //        {
            //            break;
            //        }
            //    default: break;
            //}
            //如需要使用缓存日志,则需要使用下面代码            

        }

        public bool Clear()
        {
            IDictionaryEnumerator cacheEnmu = webCache.GetEnumerator();
            while (cacheEnmu.MoveNext())
            {
                webCache.Remove(cacheEnmu.Key.ToString());
            }
            //webCache.
            //webCache = null;
           // webCache = System.Web.HttpRuntime.Cache.;
            return false;
        }

       
    }
}
